package bricks.entity;

import bricks.config.GameConstant;
import javafx.scene.image.Image;
import javafx.scene.shape.Rectangle;

public class Ball extends AbstractGraph{

    private boolean start;

    private double speedX;

    private double speedY;

    private int directionX;

    private int directionY;

    public Ball(double x, double y, double width, double height) {
        super(x, y, width, height);
        setImage(new Image("file:src/bricks/image/ball_blue.png"));
    }

    public double getSpeedX() {
        return speedX;
    }

    public void setSpeedX(double speedX) {
        this.speedX = speedX;
    }

    public double getSpeedY() {
        return speedY;
    }

    public void setSpeedY(double speedY) {
        this.speedY = speedY;
    }

    public int getDirectionX() {
        return directionX;
    }

    public void setDirectionX(int directionX) {
        this.directionX = directionX;
    }

    public int getDirectionY() {
        return directionY;
    }

    public void setDirectionY(int directionY) {
        this.directionY = directionY;
    }

    public boolean isStart() {
        return start;
    }

    public void setStart(boolean start) {
        this.start = start;
    }

    @Override
    public void setX(double x) {
        if (x < 0 || x > 640 - GameConstant.BALL_WIDTH) {
            setDirectionX(getDirectionX() * -1);
        }
        super.setX(x);
    }

    @Override
    public void setY(double y) {
        if (y < 0) {
            setDirectionY(getDirectionY() * -1);
        }
        if (y > 400 - GameConstant.HUGE_HEIGHT) {
            life = false;
        }
        super.setY(y);
    }

    public boolean collideWith(AbstractGraph graph) {
        if (graph.isLife()) {
            Rectangle boundBox = getBoundBox();
            if (boundBox.intersects(graph.x, graph.y, graph.getWidth(), graph.getHeight())) {
                if (graph instanceof Brick) {
                    graph.life = false;
                }
                setDirectionY(getDirectionY() * -1);
                return true;
            }
            return false;
        }
        return false;

    }
}
